package com.team.cloudsharing.controller.sharefolder;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.team.cloudsharing.dto.FolderSharing;
import com.team.cloudsharing.dto.Message;
import com.team.cloudsharing.dto.User;
import com.team.cloudsharing.service.sharefolder.ShareFolderService;

@RestController
@RequestMapping("/sharefolder")
public class ShareFolderControllerImpl implements ShareFolderController {

	final static Logger logger = Logger.getLogger(ShareFolderControllerImpl.class);

	@Autowired
	ShareFolderService shareFolderService;
	
	@Override
	@RequestMapping(value = "/shareFolderToListUser", method = RequestMethod.POST)
	public ResponseEntity<Message> shareFolderToListUser(@RequestBody FolderSharing folderSharing) {
		logger.info("POST /folder/shareFolderToListUser - folderSharing: " + folderSharing.toString());
		String statusShareFolderToListUser = shareFolderService.shareFolderToListUser(folderSharing);
		return new ResponseEntity<Message>(new Message(statusShareFolderToListUser), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "getShareFolderInformation", method = RequestMethod.GET)
	public ResponseEntity<List<User>> getShareFolderInformation(
			@RequestParam("userEmail") String userEmail,
			@RequestParam("folderName") String folderName,
			@RequestParam("folderPath") String folderPath) {
		logger.info("getShareFolderInformation Controller");
		List<User> listUsers = shareFolderService.getShareFolderInformation(
				userEmail, folderName, folderPath);
		return new ResponseEntity<List<User>>(listUsers, HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "leaveOrUnShareFolder", method = RequestMethod.POST)
	public ResponseEntity<Message> leaveOrUnShareFolder(@RequestParam("userEmailOfRequester") String userEmailOfRequester, @RequestParam("userEmailOfDocument") String userEmailOfDocument,
			@RequestParam("folderName") String folderName, @RequestParam("folderPath") String folderPath, @RequestParam("action") String action) {
		shareFolderService.leaveOrUnShareFolder(userEmailOfRequester, userEmailOfDocument, folderName, folderPath, action);
		return new ResponseEntity<Message>(new Message("Leave or Unshare Successful"),
				HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/leaveOrUnShareWeb", method = RequestMethod.GET)
	public ResponseEntity<Message> leaveOrUnShareWeb(@RequestParam("userEmailOfRequester") String userEmailOfRequester, @RequestParam("folderName") String folderName, @RequestParam("folderPath") String folderPath) {
		logger.info("leave Unshare Web");
		shareFolderService.leaveOrUnShareWeb(userEmailOfRequester, folderName, folderPath);
		return new ResponseEntity<Message>(new Message("Leave or Unshare Successful"),
				HttpStatus.OK);
	}

}
